package org.example2;

import java.util.Random;

public class ThreadJoin {
    /*
    在thread1线程执行join后,main线程状态为WAITING
     */
    public static void main(String[] args) throws Exception {

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                new ThreadJoin().longConsumer();
            }
        });

        thread1.start();
        thread1.join();//在join执行后,main线程状态为WAITING,通过jstack查看
        System.out.println("main线程结束了");
    }

    //长耗时方法,大概50秒.可以多次循环调用
    public void longConsumer() {
        Long _long = 0L;
        long start = System.currentTimeMillis();
        for (long i = 0; i <=999_999_999L; i++) {
            Random random = new Random();
            int seed = random.nextInt(100);
            Random random1 = new Random(seed);
            int i1 = random1.nextInt(100);
            _long = i1 + _long;
        }
        long end = System.currentTimeMillis();

        System.out.println("程序耗时:" + (end - start) + "毫秒-----运行结果是:"+_long);
    }

}
